<?php
/**
 * 提供给其他地三方使用API接口
 * @author coldarmy
 * **/
apf_require_controller('Oa_Attendance_Abstract');
apf_require_class('Bll_Attendance_UserInfo');
apf_require_class('Bll_Attendance_AccessDayRecord');

class Oa_Attendance_MobileAPIController {

    private $bll_user;
    private $bll_org;

    public function handle_request() {
        $request = APF::get_instance()->get_request();
        $params = $request->get_parameters();
        $this->bll_user = new Bll_Attendance_UserInfo();

        $echo = $this->get_user_info($params);
        echo json_encode($echo);
        exit;
    }

    private function get_user_info($params) {
        $result = array();
        if(empty($params) || empty($params['code'])) {
            return $result;
        }
        $auth = $params['code'];
        $action = 'checkin';
        if ($params['action'] == 'list') {
            $action = $params['action'];
        }

        $columns = explode(',','code');
        $where[$this->build_where($columns,$params['code'])] = 1;
        $user = $this->bll_user->get_user_by_condition($where);
        if(empty($user)) {
            echo json_encode(array('msg'=>"Don't have this user"));
            exit();
        }
        $user_id = $user[0]['user_id'];
        $bll = new Bll_Attendance_AccessDayRecord();
        if ($action == 'checkin') {
            $data['access_date'] = date('Ymd');
            $data['access_time'] = date('Hi');
            $a = $bll->insert_data_to_day_record($data, $user_id);
            $data['machine_num'] = '1';
            $data['code'] = $auth;
            $b = $bll->insert_data_to_all_record($data);
            if ($a && $b) {
                $result = array('msg'=>"ok");
            } else {
                $result = array('msg'=>"fail");
            }
            echo json_encode($result);
            die();
        } elseif($action == 'list') {
            $data['access_date'] = date('Ymd');
            $data['code'] = $auth;
            $tmp = $bll->get_all_record($data);
            if($tmp) {
                $result = array();
                //echo "用户:{$user[0]['name']} {$val['access_date']} 打卡时间点记录数据:<br>";
                foreach ($tmp as $key => $val) {
                    //$num = $key + 1;
                    //echo "{$num} : {$val['access_time']}<br>";
                    $result[] = array('access_time'=>$val['access_time'],'access_date'=>$val['access_date']);
                }
                echo json_encode($result);
            }
            die();
        }
        return $result;
    }

    /**
     * 获取下属信息
     * @param int $user_id
     * @return array
     * **/
    private function get_subordinate($user_id) {
        $result = array();
        if(empty($user_id)) {
            return $result;
        }
        $tmp = $this->bll_org->get_subordinate_by_user_id($user_id);
        if(empty($tmp)) {
            return $result;
        }
        $ids =  array();
        foreach ($tmp as $key => $val) {
            $ids[] = $val['user_id'];
        }
        $back = $this->bll_user->get_user_by_ids($ids);
        if(empty($back)) {
            return $result;
        }
        foreach ($back as $k => $v) {
            $result[$k]['user_id'] = $v['user_id'];
            $result[$k]['name'] = $v['name'];
            $result[$k]['code'] = $v['code'];
        }
        return $result;
    }

    /**
     * 获取上级信息
     * **/
    private function get_superior($user_id) {
         $result = array();
        if(empty($user_id)) {
            return $result;
        }
        $tmp = $this->bll_org->get_sup_id($user_id,1);
        $tmp = $this->bll_user->get_user_by_ids($tmp);
        if (empty($tmp)) {
            return $result;
        }
        $result['name'] = $tmp[0]['name'];
        $result['code'] = $tmp[0]['code'];
        $result['user_id'] = $tmp[0]['user_id'];
        return $result;
    }

    /**
     * 构建查询条件
     * @param array $data
     * @param string $keyword
     * @return string
     * **/
    private function build_where($data,$keyword) {
        $result = ' 1 != ? ';
        if(empty($data) || empty($keyword)) {
            return $result;
        }
        foreach ($data as $key => $val) {
            if(in_array($val,array('code','name','email'))) {
                $result .= ' OR '.$this->build_columns_condition($val,$keyword);
            }
            if($val == 'user_id') {
                $result = ' OR'.$this->build_columns_condition($val,$keyword);
                break;
            }
        }
        return $result;
    }

    /**
     * 构建字段查询的组合条件
     * @param string $columns
     * @param string $keyword
     * @return string
     * **/
    private function build_columns_condition($columns,$keyword) {
        $result = '';
        if($columns == 'code') {
            $result = " code = '{$keyword}' ";
        } elseif ($columns == 'name') {
            $result = " name like '%".$keyword."%' ";
        } elseif ($columns == 'email') {
            $result = " email like '%".$keyword."%' ";
        } elseif ($columns == 'user_id') {
            $result = " code = '{$keyword}' ";
        }
        return $result;
    }



}
